package social.hub.google.orkut;

import java.util.List;

import javax.ws.rs.core.MediaType;

import social.hub.util.RPCData;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

/**
 * http://docs.opensocial.org/display/OSD/OpenSocial+REST+Developer%27s+Guide+%
 * 28v0.9%29 html#rfc.section.7.1
 * 
 * @author Andre
 */
public class OrkutClientImpl implements OrkutClient {

	private static final String ORKUT_RPC_URL = "http://www.orkut.com/social/rpc/";

	private static final String ORKUT_REST_SCOPE = "https://orkut.gmodules.com/social/rest/";
	private static final String ORKUT_RPC_SCOPE = "http://orkut.gmodules.com/social";

	private static final String SELF_ID = "@me";

	private static final String VIEWER = "@viewer";

	private static final String SELF_APP = "@app";
	private static final String SELF_GROUP = "@self";
	private static final String FRIENDS = "@friends";

	private Client client;

	public OrkutClientImpl(String consumerToken, String consumerTokenSecret, String accessToken,
			String accessTokenSecret) {

		ClientConfig clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);

		client = Client.create(clientConfig);

		OAuthParameters oauthParameters = new OAuthParameters().consumerKey(consumerToken).token(
				accessToken);
		OAuthSecrets oauthSecrets = new OAuthSecrets().consumerSecret(consumerTokenSecret)
				.tokenSecret(accessTokenSecret);

		oauthParameters.put("scope", ORKUT_RPC_SCOPE);

		client.addFilter(new LoggingFilter());

		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), oauthParameters,
				oauthSecrets);

		client.addFilter(filter);

	}

	@Override
	public List<OrkutProfile> getFriends() {
		return getFriends(SELF_ID);
	}

	@Override
	public List<OrkutProfile> getFriends(String buzzId) {

		OrkutRPCData orkutRPCData = new OrkutRPCData();
		orkutRPCData.setGroupId(FRIENDS);
		orkutRPCData.setUserId(VIEWER);

		RPCData rpcData = new RPCData("people.get", orkutRPCData);

		OrkutFriendsResponse response = client.resource(ORKUT_RPC_URL)
				.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE)
				.post(OrkutFriendsResponse.class, rpcData);

		return response.getData().getList();

	}

	@Override
	public OrkutProfile getUserProfile() {
		return getUserProfile(SELF_ID);
	}

	@Override
	public OrkutProfile getUserProfile(String orkutId) {

		OrkutRPCData orkutRPCData = new OrkutRPCData();
		orkutRPCData.setGroupId(SELF_GROUP);
		orkutRPCData.setUserId(VIEWER);

		RPCData rpcData = new RPCData("people.get", orkutRPCData);

		OrkutProfileResponse response = client.resource(ORKUT_RPC_URL)
				.type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON_TYPE)
				.post(OrkutProfileResponse.class, rpcData);

		OrkutProfile orkutProfile = response.getData();

		return orkutProfile;

	}

	@Override
	public void updateStatus(String status) {
		updateStatus(status, SELF_ID);
	}

	@Override
	/**
	 * http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/rpc-protocol
	 */
	public void updateStatus(String status, String orkutId) {

		OrkutActivity activity = new OrkutActivity();
		activity.setTitle("Titulo");
		activity.setBody(status);

		OrkutRPCData orkutRPCData = new OrkutRPCData();
		orkutRPCData.setGroupId(SELF_GROUP);
		orkutRPCData.setAppId(SELF_APP);
		orkutRPCData.setUserId(VIEWER);

		orkutRPCData.setActivity(activity);

		RPCData rpcData = new RPCData("activities.create", orkutRPCData);

		client.resource(ORKUT_RPC_URL).type(MediaType.APPLICATION_JSON_TYPE)
				.accept(MediaType.APPLICATION_JSON_TYPE).post(rpcData);

	}

}
